import router from './router'
import store from './store'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css'// Progress 进度条样式
import { Message } from 'element-ui'
import { getReToken, getToken, getCookie, setCookie, removeCookie } from '@/utils/auth' // 验权

const whiteList = ['/login'] // 不重定向白名单
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  const { path } = to
  if (path) {
    /* { roles } = await store.dispatch('getInfo')*/
    const roles = ['admin', 'edit']
    console.log('=======================')
    console.log(roles)
    const accessRoutes = await store.dispatch('generateRoutes', roles)

    // dynamically add accessible routes
    router.addRoutes(accessRoutes)
    next()
  } else {
    // other pages that do not have permission to access are redirected to the login page.
    next(`/dashboard?redirect=${to.path}`)
    NProgress.done()
  }
  NProgress.done()
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
